//
//  MessageComments.m
//  YZZX
//
//  Created by Easaa on 15/5/9.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "MessageComments.h"

@implementation MessageComments
{
    UIView  *_viewbg;
    UITextView  *_MassgeTextView;
    HXHttpRequest  *_httpRequest;
}

-(id)initWithViewComment:(CGRect)frame conmmid:(NSString *)id_massge
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.id_massge = id_massge;
        [self ceartView];
        
        
    }
    
    return self;

}

-(void)ceartView
{
    self.backgroundColor =RGBACOLOR(0, 0, 0, 0.2);
    [self addTarget:self action:@selector(Butfantion:) forControlEvents:UIControlEventTouchUpInside];
    
    _viewbg =[[UIView alloc]initWithFrame:CGRectMake(20, 64+30, Dev_ScreenWidth-40, 160)];
    
    if (isIPHone4)
    {
        _viewbg =[[UIView alloc]initWithFrame:CGRectMake(20, 64, Dev_ScreenWidth-40, 160)];

    }
   
    
    
    [_viewbg.layer setMasksToBounds:YES];
    [_viewbg.layer setCornerRadius:0]; //设置矩圆角半径
    [_viewbg.layer setBorderWidth:0];   //边框宽度
    _viewbg.backgroundColor =[UIColor whiteColor];
    [self addSubview:_viewbg];
    
    
    UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 160, 30)];
    labelTitle.font = [UIFont boldSystemFontOfSize:14.0];  //UILabel的字体大小
    labelTitle.numberOfLines = 0;  //必须定义这个属性，否则UILabel不会换行
    labelTitle.textColor =[UIColor blackColor];
    labelTitle.textAlignment = NSTextAlignmentLeft;  //文本对齐方式
    //[labelTitle setBackgroundColor:[UIColor redColor]];
    labelTitle.text =@"内容：";
    [_viewbg addSubview:labelTitle];
    
    
    
    _MassgeTextView =[[UITextView alloc]initWithFrame:CGRectMake(5,30, Dev_ScreenWidth-50, 80)];
    _MassgeTextView.tag =3;
    _MassgeTextView.delegate =self;
    [_MassgeTextView.layer setMasksToBounds:YES];
    [_MassgeTextView.layer setCornerRadius:3]; //设置矩圆角半径
    [_MassgeTextView.layer setBorderWidth:0.5];   //边框宽度
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){0, 0, 0, 0.5});

    [_MassgeTextView.layer setBorderColor:colorref];//边框颜色
    //_MassgeTextView.backgroundColor = [UIColor redColor];
    [_viewbg addSubview:_MassgeTextView];
    
    //焦点
    [_MassgeTextView becomeFirstResponder];
   
    
    
    
    
    
    
    UIButton *BottomCancel = [UIButton buttonWithType:UIButtonTypeCustom];
    BottomCancel.tag = 0 ;
    BottomCancel.frame = CGRectMake(0,120,(Dev_ScreenWidth-40)/2+0.5, 40.5);
    [BottomCancel addTarget:self action:@selector(settingButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [BottomCancel setTitle:@"取消" forState:UIControlStateNormal];
    [BottomCancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [BottomCancel setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [BottomCancel setBackgroundColor:RGBCOLOR(238, 238, 238)];
    
    [_viewbg addSubview:BottomCancel];
    
    [BottomCancel.layer setMasksToBounds:YES];
    //[BottomCancel.layer setCornerRadius:5]; //设置矩圆角半径
    [BottomCancel.layer setBorderWidth:0];   //边框宽度
    
    
    
    UIButton *BottomConfirm = [UIButton buttonWithType:UIButtonTypeCustom];
    BottomConfirm.tag = 1 ;
    BottomConfirm.frame = CGRectMake((Dev_ScreenWidth-40)/2,120, (Dev_ScreenWidth-40.5)/2+0.5, 40.5);
    [BottomConfirm addTarget:self action:@selector(settingButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [BottomConfirm setTitle:@"发送" forState:UIControlStateNormal];
    [BottomConfirm setTitleColor:RGBCOLOR(252, 117, 148) forState:UIControlStateNormal];
    [BottomConfirm setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [BottomConfirm setBackgroundColor:RGBCOLOR(238, 238, 238)];
    
    [_viewbg addSubview:BottomConfirm];
    
    [BottomConfirm.layer setMasksToBounds:YES];
    //[BottomConfirm.layer setCornerRadius:5]; //设置矩圆角半径
    [BottomConfirm.layer setBorderWidth:0];   //边框宽度

    
    

    
    
    
}

-(void)Butfantion:(UIControl *)control
{
    [self removeFromSuperview];
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder]; return NO;
    }
    return YES;
}



-(void)settingButtonClicked:(UIButton *)but
{
    //取消  确定
    if(but.tag == 0)
    {
        // 销毁
        [_MassgeTextView resignFirstResponder];
        [self removeFromSuperview];
        
    }else if (but.tag == 1)
    {
        if(_MassgeTextView.text.length <=0)
        {
        
            
            UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:nil
                                                                message:@"请输入内容!"
                                                               delegate:self
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
            
            AlertView.tag = 11;
            [AlertView show];
            
            return;
        
        }
        
        
        
        //发出留言请求
        
        NSDictionary  *dicpost =[NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithInteger:[self.id_massge integerValue]],    @"ReceiverId",
                                 _MassgeTextView.text,                    @"Content",
                                nil];
        
        
        //列表请求
        NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [XSZ_UICommon useridChangUaerdfLoginBoolString],    @"user_id",
                                   @"1",    @"site_id",
                                   dicpost,    @"post_data",nil];
    
        
        if (_httpRequest == nil)
        {
            _httpRequest = [[HXHttpRequest alloc] init];
            _httpRequest.delegate = self;
            
            
            
        }
        [_httpRequest requestForParams:dicParams andMethod:YZ_TOPICMESSAGES_SEND andTag:1001];//@"system.time"];//@"category.list"];
      
        
        [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        [_MassgeTextView resignFirstResponder];
        
    }
    
}


#pragma mark - HXHttpRequestDelegate  代理

- (void)requestFinished:(NSString *)responseString andStatusCode:(int)statusCode andTag:(NSInteger)tag
{
    [self removeFromSuperview];
    
    [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
    
        NSDictionary *dic = [[NSDictionary alloc]init];
        dic = [JsonUtil jsonToDic:responseString];
        
        // NSLog(@"---------------->>>> \n%@",dic);
    
    
}

- (void)requestFailed:(NSError *)error andTag:(NSInteger)tag
{
    [self removeFromSuperview];
    
}





@end
